home *** CD-ROM | disk | FTP | other *** search
/ Delphi Magazine Collection 2001 / Delphi Magazine Collection 20001 (2001).iso / DISKS / Issue36 / Imagelst / OverlaidImageList.pas < prev    next >
Encoding:
Pascal/Delphi Source File  |  1998-04-09  |  2.8 KB  |  82 lines

  1. unit OverlaidImageList;
  2.  
  3. interface
  4.  
  5. uses
  6.   Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs;
  7.  
  8. type
  9.   // This image list allows you to define overlays for the main images. The first
  10.   // OverlayCount images are overlayed onto the other images. At run time each
  11.   // set of images is overlayed with the overlays.
  12.   TOverlaidImageList = class(TImageList)
  13.   private
  14.     FOverlayCount: TOverlay;
  15.     NonOverlayCount: Integer;
  16.   protected
  17.     procedure Loaded; override;
  18.     function GetOverlayedImageIndex (ThisOverlayIndex: Integer; ThisImageIndex: Integer): Integer;
  19.   public
  20.     property OverlayedImageIndex [OverlayIndex: Integer; ImageIndex: Integer]: Integer read GetOverlayedImageIndex;
  21.   published
  22.     property OverlayCount: TOverlay read FOverlayCount write FOverlayCount;
  23.   end;
  24.  
  25. const
  26.   NoOverlay = -1;
  27.     
  28. implementation
  29.  
  30. function TOverlaidImageList.GetOverlayedImageIndex (ThisOverlayIndex: Integer; ThisImageIndex: Integer): Integer;
  31. // Returns the index of the image overlayed with the given overlay.
  32. begin
  33.   Assert ((ThisOverlayIndex = NoOverlay) or (ThisOverlayIndex in [0..3]), 'Overlay must be between 0 and 3.');
  34.   // The first set of images are the overlays, the 2nd set not overlayed, the 3rd set with overlay[0] and so on.
  35.   Result := (NonOverlayCount * (ThisOverlayIndex + 1)) + ThisImageIndex;
  36. end;
  37.  
  38. procedure TOverlaidImageList.Loaded;
  39. var
  40.   WorkList: TImageList;
  41.   ThisImage: Integer;
  42.   WorkBitmap: TBitmap;
  43.   ThisOverlay: Integer;
  44. begin
  45.   inherited;
  46.   
  47.   NonOverlayCount := Count - OverlayCount;
  48.   if not (csDesigning in ComponentState) and (OverlayCount > 0) then begin
  49.     if OverlayCount > Count then begin
  50.       raise EListError.Create ('Overlay count exceeds image count');
  51.     end;
  52.     WorkList := TImageList.CreateSize (Self.Width, Self.Height);
  53.     WorkBitmap := TBitmap.Create;
  54.     try
  55.       // Copy all our non-overlayed images to a temp image list.
  56.       WorkList.Assign (Self);
  57.       // overlay each image in turn and add it to the working list.
  58.       for ThisOverlay := 0 to OverlayCount - 1 do begin
  59.         // Register the overlay.
  60.         Overlay (ThisOverlay, ThisOverlay);
  61.         for ThisImage := 0 to NonOverlayCount - 1 do begin
  62.           // Clear out the bitmap.
  63.           WorkBitmap.Create;
  64.           WorkBitmap.Height := Self.Height;
  65.           WorkBitmap.Width := Self.Width;
  66.           // Overlay the main image with it's overlay onto the bitmap.
  67.           DrawOverlay (WorkBitmap.Canvas, 0, 0, OverlayCount + ThisImage, ThisOverlay);
  68.           // Add this to the working list.
  69.           WorkList.Add (WorkBitmap, nil);
  70.         end;
  71.       end;
  72.       // Now copy the overwrite me with the working images.
  73.       Assign (WorkList); 
  74.     finally
  75.       WorkList.Free;
  76.       WorkBitmap.Free;
  77.     end;
  78.   end;
  79. end;
  80.  
  81. end.
  82.